Imports MIMS.WRS.Entities
Imports MIMS.WRS.Business

Public Class UnitSettingDetail1
    Inherits System.Web.UI.UserControl

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents ltrMessage As System.Web.UI.WebControls.Literal
    Protected WithEvents Requiredfieldvalidator2 As System.Web.UI.WebControls.RequiredFieldValidator
    Protected WithEvents btnOK As System.Web.UI.WebControls.Button
    Protected WithEvents btnCancel As System.Web.UI.WebControls.Button
    Protected WithEvents ddlUnits As System.Web.UI.WebControls.DropDownList
    Protected WithEvents txtCbmValue As System.Web.UI.WebControls.TextBox
    Protected WithEvents btnDelete As System.Web.UI.WebControls.Button
    Protected WithEvents reqUnit As System.Web.UI.WebControls.RequiredFieldValidator
    Protected WithEvents txtUnitMeasurement As System.Web.UI.WebControls.TextBox

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Protected mainObj As UnitSetting = Nothing

    Protected Property ReferrerUrl() As String
        Get
            Return Convert.ToString(ViewState("ReferrerUrl"))
        End Get
        Set(ByVal Value As String)
            ViewState("ReferrerUrl") = Value
        End Set
    End Property

    Protected Property UnitCode() As String
        Get
            Return Convert.toString(ViewState("UnitCode"))
        End Get
        Set(ByVal Value As String)
            ViewState("UnitCode") = Value
        End Set
    End Property

    Protected Property UnitID() As Integer
        Get
            Return Convert.ToInt32(ViewState("UnitID"))
        End Get
        Set(ByVal Value As Integer)
            ViewState("UnitID") = Value
        End Set
    End Property

    Protected Property IsSystem() As Boolean
        Get
            Return Convert.ToBoolean(IIf(ViewState("IsSystem") Is Nothing, False, ViewState("IsSystem")))
        End Get
        Set(ByVal Value As Boolean)
            ViewState("IsSystem") = Value
        End Set
    End Property

    Protected Property IsContainer() As Boolean
        Get
            Return Convert.ToBoolean(IIf(ViewState("IsContainer") Is Nothing, False, ViewState("IsContainer")))
        End Get
        Set(ByVal Value As Boolean)
            ViewState("IsContainer") = Value
        End Set
    End Property

    Protected ReadOnly Property IsAddNew() As Boolean
        Get
            If HttpContext.Current.Request.QueryString("id") Is Nothing Then
                Return True
            Else
                Return False
            End If
        End Get
    End Property

    Private Function BuildObject() As Boolean
        If IsAddNew Then
            mainObj = New UnitSetting
        Else
            mainObj = UnitSettingService.GetUnitSetting(UnitID)
            If mainObj Is Nothing Then
                Utilities.ShowMessagePage("Failed to load requested Unit Description. It may be deleted or not found")
                Return False
            End If
        End If
        Return True
    End Function

    Private Sub UpdateData(ByVal bSave As Boolean)
        If (bSave) Then
            If (txtCbmValue.Text.Trim.Length > 0) Then
                mainObj.CbmValue = Decimal.Parse(txtCbmValue.Text.Replace(",", ""))
            Else
                mainObj.CbmValue = Nulls.NullDecimal
            End If
            mainObj.UnitCode = Convert.ToString(IIf(UnitCode Is Nothing, "", UnitCode))
            mainObj.Description = txtUnitMeasurement.Text.Trim()
            mainObj.IsContainer = IsContainer
            mainObj.IsSystem = IsSystem
        Else
            If (IsAddNew) Then
                txtCbmValue.Text = ""
                txtUnitMeasurement.Text = ""
            Else
                If (mainObj.CbmValue <> Nulls.NullDecimal) AndAlso (mainObj.CbmValue <> 0D) Then
                    txtCbmValue.Text = mainObj.CbmValue.ToString(Config.DecimalFormat4)
                Else
                    txtCbmValue.Text = ""
                End If
                txtUnitMeasurement.Text = mainObj.Description
                UnitCode = Convert.ToString(IIf(mainObj.UnitCode Is Nothing, "", mainObj.UnitCode))
                IsContainer = mainObj.IsContainer
                IsSystem = mainObj.IsSystem
            End If
        End If
    End Sub

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If RequestContext.CurrentUser.IS_ADMIN = False Then
            btnDelete.Visible = False
        End If
        If Not IsPostBack Then
            Try
                UnitID = Convert.ToInt32(Request.QueryString("id").ToString())
            Catch ex As Exception
            End Try
            ReferrerUrl = Utilities.GetReferrerUrl("UnitSettings.aspx")

            If (BuildObject()) Then
                UpdateData(False)
            End If

            If IsAddNew = False Then
                btnDelete.Attributes("onclick") = "return confirm('Are you sure you want to delete?');"
                btnDelete.Visible = True
            End If
        End If
    End Sub

    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        Dim bSucceed As Boolean = True
        If (Page.IsValid) Then
            If (BuildObject()) Then
                UpdateData(True)
                If IsAddNew Then
                    Try
                        UnitSettingService.CreateUnitSetting(mainObj)
                    Catch ex As Exception
                        bSucceed = False
                        ltrMessage.Text = "An exception occurs when create an unit setting."
                    End Try
                Else
                    Try
                        UnitSettingService.UpdateUnitSetting(mainObj)
                    Catch ex As Exception
                        bSucceed = False
                        ltrMessage.Text = "An exception occurs when updating the unit setting."
                    End Try
                End If
                If (bSucceed) Then Response.Redirect(ReferrerUrl)
            End If
        End If

        'Dim bSucceed As Boolean = True
        'If (Page.IsValid) Then
        '    If (BuildObject()) Then
        '        UpdateData(True)
        '        If IsAddNew Then
        '            Try
        '                If IsUnitSettingExisted(mainObj.UnitCode) = True Then Return
        '                UnitSettingService.CreateUnitSetting(mainObj)
        '            Catch ex As Exception
        '                bSucceed = False
        '                ltrMessage.Text = "An exception occurs when create an unit setting."
        '            End Try
        '        Else
        '            Try
        '                If Not UnitCode.ToLower().Trim().Equals(mainObj.UnitCode.ToLower().Trim()) Then
        '                    If IsUnitSettingExisted(UnitCode) = True Then Return
        '                End If
        '                UnitSettingService.UpdateUnitSetting(mainObj)
        '            Catch ex As Exception
        '                bSucceed = False
        '                ltrMessage.Text = "An exception occurs when updating the unit setting."
        '            End Try
        '        End If
        '        If (bSucceed) Then Response.Redirect(ReferrerUrl)
        '    End If
        'End If
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Response.Redirect(ReferrerUrl)
    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        Try
            If UnitID > 0 Then
                If IsSystem Then
                    ltrMessage.Text = "Cannot delete this item. It has been in used."
                    Return
                End If
                UnitSettingService.DeleteUnitSetting(UnitID)
                Response.Redirect(ReferrerUrl)
            End If
        Catch ex As Exception
        End Try
    End Sub

    'Private Function IsUnitSettingExisted(ByVal unitCode As String) As Boolean
    '    Dim dummy As UnitSetting = UnitSettingService.GetUnitSetting(unitCode)
    '    If Not dummy Is Nothing Then
    '        ltrMessage.Text = "This unit setting has already existed. Please select another."
    '        Return True
    '    Else
    '        Return False
    '    End If
    'End Function
End Class
